/*
Step 1: compute market shares in the full population in a given period

Step 2: use output from step 1 to graph conditional market shares and baseline market shares (Figure 1a of the paper)


*/




////////////
// Step 1 //
////////////

/*
Basic summary graphs showing market share over time:
- Compare it to new user market share
- Just make a point about crestor for now

Done at the quarter level:
- So fewer issues about days supplied, etc.

Also do first time user choice over time and compare

*/


set more off

clear
use user_quarter_choice_cholesterol


// focus on cholesterol
tab indic_id
keep if indic_id == 5


egen first_q = min(quarter), by(enrolid)
gen first_choice_temp = generic_id if quarter == first_q
egen first_choice = max(first_choice_temp), by(enrolid)


merge m:1 generic_id using generic_name_mapping
keep if _merge == 3
drop _merge


// all market share

preserve
gen patients = 1
collapse (sum) patients (first) gennme generic xr, by(drugid quarter)
egen quarter_sum = sum(patients), by(quarter)

gen market_share = patients / quarter_sum

keep drugid quarter gennme generic xr market_share quarter_sum

rename market_share market_share_all
rename quarter_sum all_users

save raw_market_shares_all_users, replace

restore


// new user market share
preserve

keep if quarter == first_q

gen patients = 1
collapse (sum) patients (first) gennme generic xr, by(drugid quarter)
egen quarter_sum = sum(patients), by(quarter)

gen market_share = patients / quarter_sum

keep drugid quarter gennme generic xr market_share quarter_sum

rename market_share market_share_new
rename quarter_sum new_users

save raw_market_shares_first_time, replace

restore




////////////
// Step 2 //
////////////


set more off

clear
use user_quarter_choice_cholesterol // has drugid quarter


// BASIC: FIGURE OUT THE FIRST CHOICE OF THE USER
egen first_q = min(quarter), by(enrolid)
gen first_choice_drugid_temp = drugid if quarter == first_q
egen first_choice_drugid = max(first_choice_drugid_temp), by(enrolid)

gen first_generic = generic if quarter == first_q
sort enrolid quarter
replace first_generic = first_generic[_n-1] if enrolid == enrolid[_n-1]


gen period = quarter - first_q
gen chose_same_option = (first_choice_drugid == drugid)


drop drugid
rename first_choice_drugid drugid


merge m:1 drugid quarter using raw_market_shares_all_users, keepus(market_share_all)
keep if _merge == 3
drop _merge

merge m:1 drugid quarter using raw_market_shares_first_time, keepus(market_share_new)
keep if _merge == 3
drop _merge






/*
Collapse the results
*/

set scheme s1mono
graph set window fontface "Palatino Linotype"


keep if first_generic == 0

collapse (mean) chose_same_option market_share_all market_share_new, by(period)

replace period = period / 4


twoway (line chose_same_option period) (line market_share_all period, lpattern(dash)) ///
(line market_share_new period, lpattern(dot)), legend(lab(1 "Same Option") lab(2 "Baseline (All)") lab(3 "Baseline (New)") size(large) region(lwidth(none))) xtitle("Years Since First Prescription", size(large)) ///
ytitle("User Fraction", size(large)) ///
xlabel(,labsize(large)) ylabel(,labsize(large))

graph save reduced_form_stickiness_brand_baseline, replace
graph export reduced_form_stickiness_brand_baseline.pdf, replace 

